$(document).ready(function(){

    // Requried: Navigation bar drop-down
    $("nav ul li").hover(function() {
        $(this).addClass("active");
        $(this).find("ul").show().animate({opacity: 1}, 400);
    },function() {
        $(this).find("ul").hide().animate({opacity: 0}, 1000);
        $(this).removeClass("active");
    });

    // Requried: Addtional styling elements
    $('nav ul li ul li:first-child').prepend('<li class="arrow"></li>');
    $('nav ul li:first-child').addClass('first');
    $('nav ul li:last-child').addClass('last');
    $('nav ul li ul').parent().append('<span class="dropdown"></span>').addClass('drop');

    //getYoutube();

    $('#search').val(getParameterByName('search'));

    $('[rel="gallery"] a').lightBox();

    //seasons
    if(document.getElementById('udn_seasons_select'))
    {
        $('#udn_seasons_select').ddslick({
            width: 150,
            height: 300,
            imagePosition: "left",
            selectText: "Select Season",
            onSelected: function (data) {
                $('#udn_episodes_select').ddslick('destroy');
                getSeason(data.selectedData.value);
            }
        });
    }

});

function createDropdownEpisods()
{
    $('#udn_episodes_select').ddslick({
        width: 523,
        height: 300,
        imagePosition: "left",
        selectText: "Select..",
        onSelected: function (data) {
            var seasonData = $('#udn_seasons_select').data('ddslick');
            var s = seasonData.selectedData.value;
            var e = data.selectedData.value;
            var title = $('#torrent_episode_title').val();
            if(s != 'x')
            {
                s = s > 9 ? s : ('0' + s);
                e = e > 9 ? e : ('0' + e);
                title = title.replace(' Season','') + ' S' + s  + 'E' + e;
            }
            var movieid=getParameterByName('movieid');
            download("http://203.124.119.201", title, "@DVDRip-720p-1080p-3D@bad-word", 'tabs','DVDRip-720p-1080p','http://trendx.info/transform?download={0}&title={1}&id='+movieid);

        }
    });
}





var lastresult = '';
//search and autocomplete
$(function(){

    $("#search").focus(); //Focus on search field
    $("#search").autocomplete({
        minLength: 0,
        delay:5,
        source: function(req, responseFn) {
            $.ajax({
                url: "suggest.php",
                data: {term : req.term },
                success: function(json) {
                    if(json[0])
                        lastresult = json;
                    if(req.term.length>0)
                        responseFn(lastresult);
                    else
                        responseFn('');
                }});},
        focus: function( event, ui ) {
            //$(this).val( ui.item.value );
            return false;
        },
        select: function( event, ui ) {
            $(this).val( ui.item.value );
            if(ui.item.value.indexOf('tt') == 0)
                window.location.href = "transform?movieid=" + ui.item.value;
            else
                window.location.href = "transform?personid=" + ui.item.value;
            //$('#form').submit();
            return false;
        }
    }).data("ui-autocomplete")._renderItem = function( ul, item ) {
        var image = '<img class="imdbImage" src="'+item.img+'" />';
        var span = '<span class="imdbTitle">'+item.label+'</span>';
        var cast = '<br /><span class="imdbCast">'+item.cast+'</span>';
        var clear = '<div class="clear"></div>';

        return $("<li></li>")
            .data( "ui-autocomplete-item", item )
            .append("<a>" + image + span + cast + clear + "</a>")
            .appendTo( ul );
    };
});
function getParameterByName(name)
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if(results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function getYoutube()
{
    var term;
    if(($("#mode").html() == 'movie'))
    {
        term = $("#udn_title").html() + " movie trailer";
    }
    else if(($("#mode").html() == 'tv-series'))
    {
        term = $("#torrent_title").val() + " trailer";
    }
    else if(($("#mode").html() == 'tv-episode'))
    {
        term = $("#torrent_title").val() + "";
    }
    else if($("#mode").html() == 'person')
    {
        term = $("#udn_title").html() + " actor";
    }
    if(term != null)
    {
        searchYoutube(term, 'udn_trailer');
    }
}

function getSeason(season)
{
    var ajaxurl = '';
    if(getParameterByName('movieid') != '')
        ajaxurl = 'transform?season=' + season + '&id='
            + getParameterByName('movieid') + '&title=' + encodeURI($("#udn_title").html());
    else
        ajaxurl = 'transform?season=' + season + '&id='
            + getParameterByName('search') + '&title=' + encodeURI($("#udn_title").html());

    $.ajax({
        url: ajaxurl
    }).done(function(data)
        {
            $('#udn_episodes_select').html(data);

            createDropdownEpisods();
        });
}
